<?php
// +----------------------------------------------------------------------
// | @projectName  【order_system---点餐系统】
// +----------------------------------------------------------------------
// | @author        山西创客空间科技有限公司
// +----------------------------------------------------------------------
// | @date          2017年10月13日 星期五
// +----------------------------------------------------------------------
// | @Copyright     http://sx-ck.com All rights reserved.      
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\admin\model\Common;
use app\common\Base;
use app\common\Error;
use think\Controller;
use think\Db;
use think\Request;

class Card extends Base 
{
    private $sql = " SELECT a.*,CASE a.type WHEN 1 THEN '学生' WHEN 2 THEN '老师' ELSE '' END AS type_name,b.`name` AS class_name,c.card_code,
                    c.create_time,c.last_recharge_time,c.last_recharge_money,c.balance FROM students a LEFT JOIN class b ON a.class_id = b.id LEFT JOIN card c ON c.id = a.id ";
    // 学生绑定饭卡的接口
    public function students_add_card () 
    {
        $dbname = Db::name('card');
        // 检测学生是否已经绑定过卡
        $id = $this -> request -> post('student_id');
        $students = Db::name('students');
        if ($this -> check_student_id($students,$id) !== true) {
            return $this -> check_student_id($students,$id);
        }
        $row = $dbname -> where("id",'=',"$id") -> select();
        if($row === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        if (count($row) > 0) {
            return Base::echo_error(Error::CARD_ID_ALREDAY_EXIST);
        }
        $data['id'] = $id;

        // 检测卡号是否存在
        $code = $this -> request -> post('card_code');
        if(Common::check_empty($code)) {
            return Base::echo_error(Error::CARD_ID_IS_EMPTY);
        }
        $row = $dbname -> where("card_code",'=',"$code") -> select();
        if($row === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        if (count($row) > 0) {
            return Base::echo_error(Error::CARD_ID_ALREDAY_EXIST);
        }
        $data['card_code'] = $code;
        // 卡创建的时间
        $data['create_time'] = date('Y-m-d H:i:s',time());
        
        // 存入卡的充值时间以及充值金额
        $recharge_money = $this -> request -> post('recharge_money');
        if (Common::check_empty($recharge_money) === true) {
            $data['last_recharge_time'] = '';
            $data['last_recharge_money'] = 0;
        } else {
            $data['last_recharge_time'] = date('Y-m-d H:i:s',time());
            $data['last_recharge_money'] = $recharge_money;
        }
        $data['balance'] = $data['last_recharge_money'] + 0;

        if ($dbname -> insert($data) == 1) {
            return Base::echo_success(Error::ADD_SUCCESS);
        } else {
            return Base::echo_error(Error::ADD_ERROR);
        }

    }

    // 查询学生信息的接口
    public function student_info () 
    {
        $dbname = Db::name('students');
        $id = $this -> request -> post('student_id');
        if ($this -> check_student_id($dbname,$id) === true) {
            $row = $dbname -> query($this -> sql."WHERE a.id = '$id'");
        } else {
            return $this -> check_student_id($dbname,$id);
        }
        return Base::echo_success($row);        
    }
    // 检测学生id是否合法
    private function check_student_id ($dbname,$id) 
    {
        if(Common::check_empty($id)) {
            return Base::echo_error(Error::STUDENTS_ID_IS_EMPTY);
        }
        $row = $dbname -> where("id",'=',"$id") -> select();
        if($row === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        if(count($row) === 0) {
            return Base::echo_error(Error::STUDENTS_ID_NOT_EXIST);
        }
        return true;
    }

    // 充值接口
    public function recharge () 
    {
        $students = Db::name('students');
        $card = Db::name('card');

        $id = $this -> request -> post('student_id');
        if ($this -> check_student_id($students,$id) === true) {
            $data['id'] = $id;
        } else {
            return $this -> check_student_id($students,$id);
        }

        $recharge_money = $this -> request -> post('recharge_money');
        if (Common::check_empty($recharge_money) === true) {
            return Base::echo_error(Error::RECHARGE_MONEY_IS_EMPTY);
        } else {
            $data['last_recharge_time'] = date('Y-m-d H:i:s',time());
            $data['last_recharge_money'] = $recharge_money;
        }
        // 查询余额
        $balance = $card -> where("id",'=',"$id") -> field('balance') -> find();
        if ($balance === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        // 进行余额运算
        $data['balance'] = $balance['balance'] + $recharge_money;
        if ($card -> update($data) !== false) {
            return Base::echo_success(Error::RECHARGE_SUCCESS);
        } else {
            return Base::echo_error(Error::RECHARGE_ERROR);                
        }
    }

    // 消费成功扣款的接口
    public function chargeback () 
    {
        $card = Db::name('card');
        // 接收的卡 的id
        $id = $this -> request -> post('student_id');
        if ($this -> check_student_id($card,$id) === true) {
            $data['id'] = $id;
        } else {
            return $this -> check_student_id($card,$id);
        }

        $order_price = $this -> request -> post('order_price');
        if (Common::check_empty($order_price) === true ) {
            return Base::echo_error(Error::ORDER_PRICE_IS_EMPTY);
        }
        // 查询余额
        $balance = $card -> where("id",'=',"$id") -> field('balance') -> find();
        if ($balance === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        $balance = $balance['balance'];   
        if ($balance < $order_price) {
            return Base::echo_error(Error::BALANCE_IS_NOT_ENOUGH);
        } else {
            // 进行余额运算
            $data['balance'] = $balance - $order_price;
        }    
        
        if ($card -> update($data) !== false) {
            return Base::echo_success(Error::RECHARGE_BACK_SUCCESS);
        } else {
            return Base::echo_error(Error::RECHARGE_BACK_ERROR);                
        }
    
    }
    // 注销卡的接口

    public function delete () 
    {
        $card = Db::name('card');
        // 接收的卡的id，并进行验证
        $id = $this -> request -> post('student_id');
        if ($this -> check_student_id($card,$id) === true) {
            $res = $card -> where('id',"$id") -> delete();
            if (!$res) {
                return Base::echo_error(Error::DELETE_ERROR);
            } else {
                return Base::echo_success(Error::DELETE_SUCCESS);
            }
        } else {
            return $this -> check_student_id($card,$id);
        }

    }
    //换卡的接口
    public function change_card () 
    {
        $card = Db::name('card');
        // 接收的卡 的id，并进行验证
        $id = $this -> request -> post('student_id');
        if ($this -> check_student_id($card,$id) === true) {
            $data['id'] = $id;
        } else {
            return $this -> check_student_id($card,$id);
        }
        

        // 检测卡号是否存在
        $code = $this -> request -> post('card_code');
        if(Common::check_empty($code)) {
            return Base::echo_error(Error::CARD_ID_IS_EMPTY);
        }
        $row = $card -> where("card_code",'=',"$code") -> select();
        if($row === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        if (count($row) > 0) {
            return Base::echo_error(Error::CARD_ID_ALREDAY_EXIST);
        }
        $data['card_code'] = $code;

        // 卡创建的时间
        $data['create_time'] = date('Y-m-d H:i:s',time());
        
        // 存入卡的充值时间以及充值金额
        $recharge_money = $this -> request -> post('recharge_money');
        if (Common::check_empty($recharge_money) === true) {
            $data['last_recharge_time'] = '';
            $data['last_recharge_money'] = 0;
        } else {
            $data['last_recharge_time'] = date('Y-m-d H:i:s',time());
            $data['last_recharge_money'] = $recharge_money;
        }

        // 查询旧卡余额
        $balance = $card -> where("id",'=',"$id") -> field('balance') -> find();
        if ($balance === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        $balance = $balance['balance'];

        $data['balance'] = $data['last_recharge_money'] + $balance;
        
        if ($card -> update($data) !== false) {
            return Base::echo_success(Error::CHANGE_CARD_SUCCESS);
        } else {
            return Base::echo_error(Error::CHANGE_CARD_ERROR);                
        }
    }

}

